import * as Cesium from 'cesium';

export class AnalysisTools {
  constructor(cesiumManager) {
    this.cesiumManager = cesiumManager;
    this.drawHandler = null;
    this.tempEntities = [];
  }

  // 点分析
  pointAnalysis() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const pickedObject = viewer.scene.pick(click.position);
      if (pickedObject && pickedObject.id) {
        const entity = pickedObject.id;
        console.log('点分析 - 选中的实体:', entity);
        
        // 显示分析结果
        const analysisEntity = viewer.entities.add({
          position: entity.position? entity.position.getValue(Cesium.JulianDate.now()) : click.position,
          label: {
            text: '点分析结果\n类型: 点要素\n属性: 示例属性',
            font: '14px sans-serif',
            fillColor: Cesium.Color.WHITE,
            outlineColor: Cesium.Color.BLACK,
            outlineWidth: 2,
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(0, -30)
          }
        });
        this.tempEntities.push(analysisEntity);
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  // 图标分析
  billboardAnalysis() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const pickedObject = viewer.scene.pick(click.position);
      if (pickedObject && pickedObject.id) {
        const entity = pickedObject.id;
        console.log('图标分析 - 选中的实体:', entity);
        
        // 显示分析结果
        const position = entity.position ? entity.position.getValue(Cesium.JulianDate.now()) : click.position;
        const analysisEntity = viewer.entities.add({
          position: position,
          label: {
            text: '图标分析结果\n类型: 图标要素\n属性: 示例属性',
            font: '14px sans-serif',
            fillColor: Cesium.Color.WHITE,
            outlineColor: Cesium.Color.BLACK,
            outlineWidth: 2,
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(0, -30)
          }
        });
        this.tempEntities.push(analysisEntity);
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  // 线分析
  lineAnalysis() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const pickedObject = viewer.scene.pick(click.position);
      if (pickedObject && pickedObject.id) {
        const entity = pickedObject.id;
        console.log('线分析 - 选中的实体:', entity);
        
        // 显示分析结果
        let position = click.position;
        if (entity.polyline && entity.polyline.positions) {
          const positions = entity.polyline.positions.getValue(Cesium.JulianDate.now());
          if (positions && positions.length > 0) {
            position = positions[Math.floor(positions.length/2)];
          }
        }
        
        const analysisEntity = viewer.entities.add({
          position: position,
          label: {
            text: '线分析结果\n类型: 线要素\n长度: 示例长度\n属性: 示例属性',
            font: '14px sans-serif',
            fillColor: Cesium.Color.WHITE,
            outlineColor: Cesium.Color.BLACK,
            outlineWidth: 2,
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(0, -30)
          }
        });
        this.tempEntities.push(analysisEntity);
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  // 面分析
  polygonAnalysis() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const pickedObject = viewer.scene.pick(click.position);
      if (pickedObject && pickedObject.id) {
        const entity = pickedObject.id;
        console.log('面分析 - 选中的实体:', entity);
        
        // 显示分析结果
        let position = click.position;
        if (entity.polygon && entity.polygon.hierarchy) {
          const hierarchy = entity.polygon.hierarchy.getValue(Cesium.JulianDate.now());
          if (hierarchy && hierarchy.positions && hierarchy.positions.length > 0) {
            position = hierarchy.positions[0];
          }
        }
        
        const analysisEntity = viewer.entities.add({
          position: position,
          label: {
            text: '面分析结果\n类型: 面要素\n面积: 示例面积\n属性: 示例属性',
            font: '14px sans-serif',
            fillColor: Cesium.Color.WHITE,
            outlineColor: Cesium.Color.BLACK,
            outlineWidth: 2,
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(0, -30)
          }
        });
        this.tempEntities.push(analysisEntity);
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  getTempEntities() {
    return this.tempEntities;
  }

  setTempEntities(entities) {
    this.tempEntities = entities;
  }

  clear() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    this.tempEntities.forEach(entity => {
      viewer.entities.remove(entity);
    });
    
    this.tempEntities = [];
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
      this.drawHandler = null;
    }
  }
}